package com.hdy.orderforthree.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.hdy.common.AndroidUtils;
import com.hdy.common.loadimage.LoadImageController;
import com.hdy.net.order.bean.DishBean;
import com.hdy.orderforthree.R;
import com.hdy.orderforthree.R2;

import java.util.List;

import butterknife.BindView;

public class FoodAdapter extends BaseAdapter {

    static class ViewHolder extends BaseAdapter.ViewHolder{

        @BindView(R2.id.food_image) ImageView mFoodImage;
        @BindView(R2.id.food_name) TextView mFoodName;
        @BindView(R2.id.food_price) TextView mFoodPrice;
        @BindView(R2.id.food_plus) ImageView mFoodPlus;
        @BindView(R2.id.food_qty) TextView mFoodQty;
        @BindView(R2.id.food_minus) ImageView mFoodMinus;

        public ViewHolder(View view){
            super(view);
        }
    }

    public FoodAdapter(List list){
        super(R.layout.three_food_item, list);
    }

    @Override
    public BaseAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(mLayoutId, parent, false);
        BaseAdapter.ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(BaseAdapter.ViewHolder holder, int position) {
        DishBean food = (DishBean) mList.get(position);
        FoodAdapter.ViewHolder foodHolder = (FoodAdapter.ViewHolder) holder;
        int resId = AndroidUtils.getDrawableResourceId(foodHolder.mFoodImage.getContext(),"image" + position);
        LoadImageController.getInstance().loadImage(resId, foodHolder.mFoodImage);
        foodHolder.mFoodName.setText(food.getName());

        String temp = foodHolder.mFoodPrice.getContext().getResources().getString(R.string.food_price);
        foodHolder.mFoodPrice.setText(String.format(temp, (int) food.getPrice()));

        foodHolder.mFoodPlus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.OnPlusClick(v, holder, position, mList.get(position));
                }
            }
        });

        foodHolder.mFoodMinus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.OnMinusClick(v, holder, position, mList.get(position));
                }
            }
        });

        foodHolder.mFoodQty.setText(String.valueOf(food.getQty()));
        if (food.getQty() > 0) {
            foodHolder.mFoodQty.setVisibility(View.VISIBLE);
            foodHolder.mFoodMinus.setVisibility(View.VISIBLE);
        } else {
            foodHolder.mFoodQty.setVisibility(View.INVISIBLE);
            foodHolder.mFoodMinus.setVisibility(View.INVISIBLE);
        }
    }
}
